package com.example.fw;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;

public class HelperWithWebDriverBase extends HelperBase {
	
	
	private WebDriver driver;
	
	public HelperWithWebDriverBase(ApplicationManager manager) {
		super(manager);
		driver = manager.getWebDriverHelper().getDriver();
	}
	
	protected boolean isElementPresent(By by) {
		try {
			driver.findElement(by);
			return true;
		} catch (NoSuchElementException e) {
			return false;
		}
	}
	
	protected void type(By locator, String name) {
		driver.findElement(locator).clear();
		driver.findElement(locator).sendKeys(name);
	}
	
	protected void select(By locator, String optionText) {
		new Select(driver.findElement(locator)).selectByVisibleText(optionText);
	}
	
	protected WebElement findElement(By locator) {
		try{
			return driver.findElement(locator);
		} catch (Exception e) {
			return null;
		}

	}
	
	public List<WebElement> findElements(By locator) {
		return driver.findElements(locator);
	}
	
	protected void openUrl(String string) {
		driver.get(manager.getProperty("baseUrl")+string);
	}
	
	protected void openAbsoluteUrl(String string) {
		driver.get(string);
	}
		
	protected void click(By locator) {
		findElement(locator).click();
	}
}
